package com.whl.sm.controller.repair;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.repair.RepairApply;
import com.whl.sm.model.repair.RepairTask;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.repair.RepairApplyService;
import com.whl.sm.service.repair.RepairTaskService;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.vo.RepairTaskIInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-28
 */
@RestController
@RequestMapping("/repair-task")
public class RepairTaskController extends BaseController {

    @Lazy
    @Autowired
    private RepairTaskService repairTaskService;

    @Autowired
    private OrgUserInfoService orgUserInfoService;

    @Lazy
    @Autowired
    private RepairApplyService repairApplyService;

    @RequestMapping("/list")
    public Result list(HttpServletRequest request){
        QueryWrapper queryWrapper = new QueryWrapper();
        Page<Object> listPage = new Page<>();
        String likeName = request.getParameter("likeName");
        if (Objects.nonNull(likeName) && !likeName.equals("undefined") && !likeName.equals("")) {
            List<OrgUserInfo> likeStaffs  = orgUserInfoService.list(new QueryWrapper<OrgUserInfo>().like("user_name",likeName));
            if (likeStaffs.size()==0){
                return Result.ok(listPage);
            }
            List<String> linkStaffIds = likeStaffs.stream().map(OrgUserInfo::getUserId).collect(Collectors.toList());
            queryWrapper.in("staff_id",linkStaffIds);
        }
        String step = request.getParameter("step");
        if (Objects.nonNull(step) && !step.equals("undefined") && !step.equals("")) {
            List<RepairApply> conditionApply = repairApplyService.list(new QueryWrapper<RepairApply>().eq("step",step));
            if(conditionApply.size()==0){
                return Result.ok();
            }
            List<String> linkApplyIds = conditionApply.stream().map(RepairApply::getId).collect(Collectors.toList());
            queryWrapper.in("link_apply_id",linkApplyIds);
        }

        listPage = repairTaskService.page(getPage(),queryWrapper);
        List<Object> resultList =  listPage.getRecords().stream().map(obj->{
            RepairTask task = (RepairTask)obj;
            RepairApply linkApply = repairApplyService.getById(task.getLinkApplyId());
            OrgUserInfo staff = orgUserInfoService.getByUserId(task.getStaffId());
            OrgUserInfo operator = orgUserInfoService.getByUserId(task.getOperator());
            return new RepairTaskIInfoVo(task,linkApply,staff,operator);
        }).collect(Collectors.toList());
        listPage.setRecords(resultList);
        return Result.ok(listPage);
    }


    @RequestMapping("/getByCurrent")
    public Result getByCurrent(@RequestParam("type") String type){
        if (Objects.isNull(type)){
            return Result.fail();
        }
        if (type.equals("notFin")){
            return repairTaskService.getNotFinByCurrent(getPage());
        }
        else {
            return repairTaskService.getFinByCurrent(getPage());
        }
    }

    @RequestMapping("/confirm")
    public Result confirmTask(@RequestParam("id") String id,@RequestParam("feedBack")String feedBack){
        return repairTaskService.confirmTask(id,feedBack);
    }

    @RequestMapping("/complete")
    public Result completeTask(@RequestParam("id") String id){
        return repairTaskService.completeTask(id);
    }


    @RequestMapping("/info/{id}")
    public Result applyInfo(@PathVariable("id") String id){
        RepairTask repairTask = repairTaskService.getById(id);
        OrgUserInfo staff = orgUserInfoService.getByUserId(repairTask.getStaffId());
        repairTask.setStaffName(staff.getUserName());
        return Result.ok(repairTask);
    }

    @RequestMapping("/update")
    public Result updateTask (@RequestBody RepairTask repairTask){
        return repairTaskService.update(repairTask);
    }

    @RequestMapping("/appraise")
    public Result appraise (@RequestParam("id") String id,@RequestParam("source")Double source,@RequestParam("appraise")String appraise){
        return repairTaskService.appraise(id,source,appraise);
    }

    @RequestMapping("/wxUpdate")
    public Result wxUpdateTask (@RequestParam("id") String id,@RequestParam("feedBack")String feedBack){
        RepairTask repairTask = repairTaskService.getById(id);
        repairTask.setRepairFeedback(feedBack);
        return repairTaskService.update(repairTask);
    }


    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            repairTaskService.update(new UpdateWrapper<RepairTask>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

}
